package com.happyfamily.springboot.crowdsourcingplatform.service.sun.impl;

import com.happyfamily.springboot.crowdsourcingplatform.dao.sun.AllianceDao;
import com.happyfamily.springboot.crowdsourcingplatform.dao.sun.TenderDao;
import com.happyfamily.springboot.crowdsourcingplatform.model.Alliance;
import com.happyfamily.springboot.crowdsourcingplatform.model.Need;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysUser;
import com.happyfamily.springboot.crowdsourcingplatform.model.Tender;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.TenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class TenderServiceImpl implements TenderService
{
	@Autowired(required = false)
	private TenderDao tenderDao;

	@Autowired(required = false)
	private AllianceDao allianceDao;

	@Override
	public boolean addTender(Tender tender)
	{
		boolean flag=false;
		int n= tenderDao.addTender(tender);
		if(n>0)
		{
			flag=true;
		}
		return flag;
	}

	@Override
	public Tender getTender(Long uid, Long nid)
	{
		return tenderDao.getTender(uid, nid);
	}



	@Override
	public boolean deleteTende(Long uid, Long nid)
	{
		boolean flag=false;
		int n=tenderDao.deleteTender(uid, nid);
		if(n>0)
		{
			flag=true;
		}
		return flag;
	}

	@Override
	public String checkTender(Long uid, Long nid)
	{
		String str="yes";
		Tender tender=tenderDao.getTender(uid, nid);
		if(tender!=null)
		{
			str="个人";
		}
		else
		{
			List<Alliance> list=allianceDao.getMyAlliance(uid);
			for (Alliance alliance : list)
			{
				Tender tender2=tenderDao.getTender(alliance.getAid(), nid);
				if(tender2!=null)
				{
					str=""+alliance.getAid();
					break;
				}
			}
		}
		return str;
	}
}